package com.pengbei.enshrine.common.dialog;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.pengbei.enshrine.R;

public class LoadingDialog extends Dialog {

    private Context context;
    private ImageView img_payloading;

    private ObjectAnimator animator = null;

    public LoadingDialog(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }

    public LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        init();
    }

    protected LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE); // 去除对话框标题
        setContentView(R.layout.dialog_loading); // 设置对话框布局

        initStatusBar();

        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置对话框背景为透明

        // 设置全屏宽高，调整布局参数
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.alpha = 1.0f; // 调整透明度，0.8是示例值，可以根据需要设置为其他值
        layoutParams.dimAmount = 0.3f; //dimAmount在0.0f和1.0f之间，0.0f完全不暗，1.0f全暗
        getWindow().setAttributes(layoutParams);

        img_payloading = findViewById(R.id.img_payloading);
        setupAnimation();

        setOnDismissListener(dialog -> {
            if (animator != null) {
                animator.cancel();
            }
        });
    }

    private void setupAnimation() {
        animator = ObjectAnimator.ofFloat(img_payloading, "rotation", 0, 360);
        animator.setDuration(2000);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.start();
    }

    private void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            if (window != null) {
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(Color.TRANSPARENT);

                int uiOptions = window.getDecorView().getSystemUiVisibility();
                uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                window.getDecorView().setSystemUiVisibility(uiOptions);
            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            dismiss();
            return true; // 消费事件，防止传递给下一层
        }
        return super.dispatchKeyEvent(event);
    }
}
